<template>
  <div class="register-container">
    <div class="common-header">
      <router-link to="/">
        <h1 class="common-header__title">汉江水环境检测中心</h1>
      </router-link>
      <div class="common-header__login">
        <div>
          <el-link href="/" :underline="false">登录</el-link>
        </div>
      </div>
    </div>
    <div class="register-form-container">
      <div class="register-form-title">
        用户注册
      </div>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请填写姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请填写手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" type="password" placeholder="请填写密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="rePassword">
          <el-input v-model="form.rePassword" type="password" placeholder="请填写密码" />
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" placeholder="请填写职位" />
        </el-form-item>
        <el-form-item label="工号" prop="no">
          <el-input v-model="form.no" placeholder="请填写工号" />
        </el-form-item>
        <el-form-item>
          <div style="text-align: center;margin: 20px;">
            <el-button :loading="loading" type="primary" style="width: 100%;" @click="onSubmit('form')">注册</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { register } from '@/api/web/register'
import { isvalidPhone } from '@/utils/validate'
import { encrypt, decrypt } from '@/utils/rsaEncrypt'
export default {
  data() {
    const validateMobile = (rule, value, callback) => {
			  if (!isvalidPhone(value)) {
			    callback(new Error('请输入正确手机号'))
			  } else {
			    callback()
			  }
    }
    const passwordRule = (rule, value, callback) => {
			  var reg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/
			  if (!reg.test(value)) {
			    // if (value.length < 6) {
			    callback(new Error('密码必须由大小写字母、数字、特殊字符组成的8-16字符组成'))
			  } else {
			    callback()
			  }
    }
    const validatePassword = (rule, value, callback) => {
			  if (value !== this.form.password) {
			    callback(new Error('两次密码输入不一致'))
			  } else {
			    callback()
			  }
    }
    return {
      form: {
        username: '',
        phone: '',
        password: '',
        rePassword: '',
        no: '',
        position: ''
      },
      rules: {
        username: [{
          required: true,
          message: '请填写姓名',
          trigger: 'blur'
        }],
        phone: [{
          required: true,
          trigger: 'blur',
          validator: validateMobile
        }],
        password: [{
          required: true,
          message: '请填写密码',
          trigger: 'blur'
        },
        {
          min: 8,
          max: 16,
          validator: passwordRule,
          trigger: 'blur'
        }
        ],
        rePassword: [{
					    required: true,
					    message: '请再次输入密码',
					    trigger: 'blur'
					  },
					  {
					    required: true,
					    trigger: 'blur',
					    validator: validatePassword
					  }
        ],
        position: [{
					  required: true,
					  message: '请选择职位',
					  trigger: 'blur'
        }],
        no: [{
					  required: true,
					  trigger: 'blur',
					  message: '请填写工号'
        }]
      },
      loading: false
    }
  },
  methods: {
    onSubmit(form) {
      this.$refs.form.validate(valid => {
        const user = {
          phone: this.form.phone,
          password: this.form.password,
          username: this.form.username,
          no: this.form.no
        }
        if (valid) {
          const form = JSON.parse(JSON.stringify(this.form))
          const originPassword = form.password
          user.phone = encrypt(user.phone)
          user.password = encrypt(user.password)
          user.username = encrypt(user.username)
          if (user.no) user.no = encrypt(user.no)
          this.loading = true
          register(user).then((res) => {
            this.form.password = originPassword
            this.loading = false
            this.$router.push({
              path: '/hj-home'
            })
            // if (res.status === 201) {
            //   this.$router.push({
            //     path: '/hj-home'
            //   })
            // } else {
            //   user.password = ''
            //   user.rePassword = ''
            // }
          }).catch(() => {
            user.password = originPassword
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scope>
	body {
		background: #f6f6f6;
	}
	.common-header {
    height: 65px;
    line-height: 65px;
    border-bottom: 1px solid #D7D8D9;
	display: block;
	.common-header__title {
    display: inline-block;
    margin: 0;
    color: #006699;
    font-weight: 600;
    line-height: 65px;
    font-size: 22px;
    font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
		float: left;
	}
	.el-link{
		color: #606266;
	}
	.common-header__login{
		display: inline-block;
    margin-right: 20px;
		float: right;
	}
}
	.register-container {
		width: 100%;
		background: #f6f6f6;

		.register-form-container {
			max-width: 1000px;
			background: #ffffff;
			margin: 80px auto;
			padding: .5rem 0;

			.register-form-title {
				width: 100%;
				height: 6rem;
				line-height: 6rem;
				text-align: center;
				font-size: 20px;
				color: #373d41;
			}

			.el-form {
				max-width: 450px;
				margin: 0 auto;
			}
		}
	}
</style>
